organizations = function() {
	//添加或者修改窗体
	var organizationAddOrModifyWin = null;
	return {
		/*--------------------------------------窗体定义--------------------------------------------*/
		panel : function() {
			var store = new TreeStore({
				fields: [
				         'id', 'name', 'enabled', 'shortName', 'remark', 
				         'ssystemType', 'ssystemTypeID'
				         ],
                proxy: {
                    type: 'ajax',
                    url: '../admin/organization.jspx?index'
                }
		    });
			var tree = new Tree({
				id: 'organizationTree',
		        store: store,
		        region: 'center',
		        stripeRows: true,
		        rootVisible: false,
		        title: '组织管理',
		        lines: true,
		        tbar: [{
		        	text: '刷新',
		        	scale: 'medium',
		        	iconCls: 'icon-refresh',
		        	handler: function() {
		        		organizations.refreshOrganizationAction();
		        	}
		        }, "-", {
		        	text: '添加',
		        	id: 'organizationAdd',
		        	disabled: true,
		        	scale: 'medium',
		        	iconCls: 'icon-add',
		        	handler: function() {
		        		organizations.addOrganizationAction();
		        	}
		        }, "-", {
		        	text: '修改',
		        	id: 'organizationEdit',
		        	disabled: true,
		        	scale: 'medium',
		        	iconCls: 'icon-edit',
		        	handler: function() {
		        		organizations.modifyOrganizationAction();
		        	}
		        }, "-", {
		        	text: '删除',
		        	id: 'organizationRemove',
		        	disabled: true,
		        	scale: 'medium',
		        	iconCls: 'icon-remove',
		        	handler: function() {
		        		organizations.delOrganizationAction();
		        	}
		        }],
		        columns: [{
		        	hidden: true,
		        	text: '序号',
		        	dataIndex: 'id'
		        }, {
	                xtype: 'treecolumn',
	                text: '组织名称',
	                flex: 0.7,
	                sortable: true,
	                dataIndex: 'name'
		        }, {
	                text: '简称',
	                flex: 0.3,
	                dataIndex: 'shortName'
		        }, {
		        	text: '等级类型',
		        	flex: 0.2,
		        	dataIndex: 'ssystemType'
		        }, {
		        	text: '是否可用',
		        	flex: 0.2,
		        	dataIndex: 'enabled',
		        	renderer: function(v) {
		        		if (v) return "<font color='blue'>是</font>";
		        		else return "<font color='red'>否</font>";
		        	}
		        }, {
		        	text: '备注',
		        	flex: 0.5,
		        	dataIndex: 'remark'
		        }],
			    listeners: {
			    	itemclick : function(fn, record, item, index, e, eOpts) {
			    		if (!record.get('leaf')) {
			    			store.getNodeById(record.get('id')).expand();
			    		}
			    	}
			    }
			});
			
			var panel = new Panel({
				layout: 'border',
				border: false,
				items: [tree]
			});
			return panel;
		},
		
		//添加或者修改窗体
		addOrModifyWin: function() {
			if (organizationAddOrModifyWin == null) {
				var store = new Store({
				    fields: ['str', 'val'],
				    data: [{
				    	'str': '是', 'val': true
				    }, {
				    	'str': '否', 'val': false
				    }]
				});
				
				var levelstore = new Store({
					fields: ['name', 'id'],
					proxy: {
	                    type: 'ajax',
	                    url: '../admin/organization.jspx?indexOFlevel'
	                },
	                autoLoad: false
				});
				
				organizationAddOrModifyWin = new Window({
					id: 'organizationAddOrModifyWin',
				    title: '编辑组织信息',
				    height: 300,
				    width: 400,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	id: 'organizationAddOrModifyForm',
				    	xtype: 'form',
				    	frame: false,
						autoHeight: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	name: 'id',
				        	hidden: true
				        }, {
				        	name: 'parentID',
				        	hidden: true
				        }, {
				        	name: 'name',
				        	fieldLabel: '组织名称',
				        	allowBlank: false
				        }, {
				        	name: 'shortName',
				        	fieldLabel: '组织简称'
				        }, {
				        	xtype: 'combo',
				        	name: 'ssystemTypeID',
				        	id: 'organizationSystemType',
				        	fieldLabel: '等级类型',
				        	store: levelstore,
				        	displayField: 'name',
				        	valueField: 'id',
				        	editable: false,
				        	queryMode: 'local',
				        	allowBlank: false
				        }, {
		                	xtype: 'combo',
			            	name: 'enabled',
			            	fieldLabel: '是否可用',
			            	store: store,
			            	displayField: 'str',
			            	valueField: 'val',
			            	editable: false,
			            	queryMode: 'local',
				        	allowBlank: false
				        }, {
				        	xtype: 'textareafield',
				        	name: 'remark',
				        	fieldLabel: '备注说明'
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		if (fom.isValid()) {
				        			organizations.formSubmitAction(fom);
				        		}
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		organizationAddOrModifyWin.hide();
				        	}
				        }]
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		Ext.getCmp('organizationAddOrModifyForm').getForm().reset();
			        	}
			        }
				});
			}
			organizationAddOrModifyWin.show();
		},
		/*----------------------------------触发事件定义-------------------------------*/
		//刷新组织事件
		refreshOrganizationAction: function() {
			Ext.getCmp('organizationTree').getStore().load();
			Ext.getCmp('organizationTree').getSelectionModel().deselectAll();
		},
		//添加组织事件
		addOrganizationAction: function() {
			var sel = Ext.getCmp('organizationTree').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要添加的上级组织！');
			} else {
				organizations.addOrModifyWin();
				var type = Ext.getCmp('organizationSystemType');
				type.setReadOnly(false);
				type.getStore().load({
					params: { 
						id: sel[0].get('ssystemTypeID'),
						dex: 0
					}
				});
				Ext.getCmp('organizationAddOrModifyForm').getForm().findField('parentID').setRawValue(sel[0].get('id'));
			}
		},
		//修改组织事件
		modifyOrganizationAction: function() {
			var tree = Ext.getCmp('organizationTree');
			var sel = tree.getSelectionModel().getSelection();
			var rnode = tree.getStore().getRootNode().getChildAt(0).get('id');
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要修改的组织！');
			} else if (sel[0].get('id') == rnode) {
				Ext.Msg.alert('提示', '您暂时无权限修改该组织！');
			} else {
				organizations.addOrModifyWin();
				//获取等级类型
				var type = Ext.getCmp('organizationSystemType');
				type.getStore().load({
					params: { 
						id: sel[0].get('ssystemTypeID'),
						dex: 1
					}
				});
				type.setReadOnly(true);
				Ext.getCmp('organizationAddOrModifyForm').getForm().loadRecord(sel[0]);
			}
		},
		//删除组织事件
		delOrganizationAction: function() {
			var tree = Ext.getCmp('organizationTree');
			var sel = tree.getSelectionModel().getSelection();
			var rnode = tree.getStore().getRootNode().getChildAt(0).get('id');
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要删除的组织！');
			} else if (sel[0].get('id') == rnode) {
				Ext.Msg.alert('提示', '您暂时无权限删除该组织！');
			} else {
				Ext.MessageBox.confirm('确认', '您确认要删除该组织吗？', function(btn) {
					if (btn == "yes") {
						var res = RequestByUrl('../admin/organization.jspx?remove', {
							id: sel[0].get('id')
						});
						if (res.ok) {
							var snode = tree.getStore().getNodeById(sel[0].get('id'));
							var pnode = snode.parentNode;
							pnode.removeChild(snode);
							if (pnode.childNodes.length == 0) {
								pnode.set('leaf', true);
								pnode.set('expandable', false);
							} 
							tree.getSelectionModel().deselectAll();
						}
						Ext.Show.msg(res.title, res.msg);
					}
				});
			}
		},
		//表单提交事件
		formSubmitAction: function(fom) {
			var url = '../admin/organization.jspx?edit';
			var id = fom.findField('id').value;
			if (id == '') {
				url = '../admin/organization.jspx?add';
			}
			FormSubmit(fom, url, {}, function(obj) {
				var sel = Ext.getCmp('organizationTree').getSelectionModel().getSelection();
				var store = Ext.getCmp('organizationTree').getStore();
				var dnode = store.getNodeById(sel[0].get('id'));
				if (id == '') {
					dnode.set('leaf', false);
					dnode.set('expandable', true);
					dnode.appendChild({
						id: obj.obj,
						name: fom.findField('name').value,
						shortName: fom.findField('shortName').value,
						enabled: fom.findField('enabled').value,
						remark: fom.findField('remark').value,
						ssystemType: fom.findField('ssystemTypeID').getRawValue(),
						ssystemTypeID: fom.findField('ssystemTypeID').value,
						leaf: true
					});
				} else {
					dnode.set('name', fom.findField('name').value);
					dnode.set('shortName', fom.findField('shortName').value);
					dnode.set('enabled', fom.findField('enabled').value);
					dnode.set('remark', fom.findField('remark').value);
				}
				organizationAddOrModifyWin.hide();
			});
		}
	};
}();